﻿using AutoMapper;
using IdentityModel.Client;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Serilog.Core;
using ShanNiang.Hosting;
using ShanNiang.Hosting.Common;
using ShanNiang.Service.Interfaces;
using ShanNiang.Service.LoginModels.Requests;
using ShanNiang.Service.LoginModels.Responses;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Xps.Packaging;

namespace ShanNiang.Service.Implementations
{
    /// <summary>
    /// 认证服务
    /// </summary>
    public class IdsService : LocalService, IIdsService
    {
        private PackingConfig _packingConfig;
        private UserContext _userContext;
        private IMapper _mapper;
        //private readonly IClientService<> _clientService;
        public IdsService(PackingConfig packingConfig, UserContext userContext,IMapper mapper, ILogger<LocalService> logger) : base(logger)
        {
            _userContext = userContext;
            _packingConfig = packingConfig;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加提供程序
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ApiResult?> AddProviderAsync(AddProviderRequest request)
        {
            var client = new ClientService<AddProviderRequest>(_packingConfig, _userContext, _mapper, Logger);
            var result = await client.GetResponse<ApiResult>(request);
            return result;
        }
        /// <summary>
        /// 绑定后再登陆
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ApiResult<WxAppLoginResponse>?> BoundLoginAsync(BoundLoginRequest request)
        {
            var client = new ClientService<BoundLoginRequest>(_packingConfig, _userContext, _mapper, Logger);
            var result = await client.GetResponse<ApiResult<WxAppLoginResponse>>(request);
            return result;
        }

        /// <summary>
        /// 检测提供程序是否正确
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult?> CheckProviderAsync(AddProviderRequest request)
        {
            var client = new ClientService<AddProviderRequest>(_packingConfig, _userContext, _mapper, Logger);
            var result = await client.GetResponse<ApiResult>(request);
            return result;
        }
        /// <summary>
        /// 获取令牌
        /// </summary>
        /// <returns></returns>
        public async Task<IdsToken?> GetTokenAsync()
        {
            var client = new ClientService<AddProviderRequest>(_packingConfig, _userContext, _mapper, Logger);
            var result = await client.GetTokenAsync();
            return result;
        }

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ApiResult?> SendSmsAsync(SendSmsRequest request)
        {
            var client = new ClientService<SendSmsRequest>(_packingConfig, _userContext, _mapper, Logger);
            //var url = request.GetUrl(_packingConfig.IdentityServer);
            var result = await client.GetResponse<ApiResult>(request);
            return result;
        }
        /// <summary>
        /// 微信登登陆
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<WxAppLoginResponse>?> WxLoginAsync(WxLoginRequest request)
        {
            var client = new ClientService<WxLoginRequest>(_packingConfig, _userContext, _mapper, Logger);
            //var url = request.GetUrl(_packingConfig.IdentityServer);
            var result = await client.GetResponse<ApiResult<WxAppLoginResponse>>(request);
            return result;
        }
    }
}
